package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;

import javax.servlet.http.HttpServletResponse;

import com.xiaoyu.lingdian.core.mybatis.page.Page;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;

import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CoreRideActive;
import com.xiaoyu.lingdian.entity.CoreRideRecord;
import com.xiaoyu.lingdian.entity.CoreUser;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CoreRideActiveService;
import com.xiaoyu.lingdian.service.CoreRideRecordService;
import com.xiaoyu.lingdian.service.CoreUserService;
import com.xiaoyu.lingdian.vo.CoreRideRecordVO;

@Controller
@RequestMapping(value="/coreRideRecord")
public class CoreRideRecordController extends BaseController {

	/**
	* CoreRideRecord表
	*/
	@Autowired
	private CoreRideRecordService coreRideRecordService;
	
	/**
	* CoreRideActive表
	*/
	@Autowired
	private CoreRideActiveService coreRideActiveService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	 * 用户表
	 */
	@Autowired
	private CoreUserService coreUserService;
	
	/**
	* 报名社区活动
	*
	* @param crraeUuid 活动UUID
	* @param crrrdUser 下单用户UUID
	*/
	@RequestMapping(value="/add/coreRideRecord", method=RequestMethod.POST)
	public void addCoreRideRecord (String crraeUuid, String crrrdUser, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin addCoreRideRecord");
		if (StringUtils.isBlank(crraeUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择活动！"), response);
			return;
		}
		if (StringUtils.isBlank(crrrdUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请先登录！"), response);
			return;
		}
		CoreRideActive coreRideActive = new CoreRideActive();
		coreRideActive.setCrraeUuid(crraeUuid);
		coreRideActive = coreRideActiveService.getCoreRideActive(coreRideActive);
		if (coreRideActive == null) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "活动不存在！"), response);
			return;
		}
		CoreUser coreUser = new CoreUser();
		coreUser.setCrusrUuid(crrrdUser);
		coreUser = coreUserService.getCoreUser(coreUser);
		if (coreUser == null) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "用户不存在！"), response);
			return;
		}
		
		CoreRideRecord coreRideRecord = new CoreRideRecord();
		String uuid = RandomUtil.generateString(16);
		coreRideRecord.setCrrrdUuid(uuid);
		coreRideRecord.setCrrrdUdate(new Date());
		coreRideRecord.setCrrrdStatus(1);
		coreRideRecord.setCrrrdActive(crraeUuid);
		coreRideRecord.setCrrrdCdate(new Date());
		coreRideRecord.setCrrrdTitle(coreRideActive.getCrraeTitle());
		coreRideRecord.setCrrrdTotal(coreRideActive.getCrraeValue());
		coreRideRecord.setCrrrdUser(crrrdUser);
		coreRideRecord.setCrrrdUserName(coreUser.getCrusrName());
		coreRideRecord.setCrrrdRemarks("");
		coreRideRecordService.insertCoreRideRecord(coreRideRecord);
		
		List<CoreAttachment> attachmentList = coreAttachmentService.findCoreAttachmentByCnd(crraeUuid);
		
		// 添加社区活动封面
		if (attachmentList.size() > 0) {
			CoreAttachment coreAttachment;
			for (int i = 0; i < attachmentList.size(); i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment = attachmentList.get(i);
				CoreAttachment attachment = new CoreAttachment();
				attachment.setCratmCdate(new Date());
				attachment.setCratmDir(coreAttachment.getCratmDir());
				attachment.setCratmExtension(coreAttachment.getCratmExtension());
				attachment.setCratmBusUuid(uuid);
				attachment.setCratmFileName(coreAttachment.getCratmFileName());
				attachment.setCratmHeight(coreAttachment.getCratmHeight());
				attachment.setCratmStatus(coreAttachment.getCratmStatus());
				attachment.setCratmType(coreAttachment.getCratmType());
				attachment.setCratmWidth(coreAttachment.getCratmWidth());
				String uuid1 = RandomUtil.generateString(16);
				attachment.setCratmUuid(uuid1);
				coreAttachmentService.insertCoreAttachment(attachment);
			}
		}		
		
		//更新社区活动的累计报名人数
		coreRideActiveService.updateCrraeCount(coreRideActive);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "报名成功!"),response);
		logger.info("[CoreRideRecordController]:end addCoreRideRecord");
	}
	
	/**
	* 修改
	*
	* @param crrrdUuid 标识UUID
	* @param crrrdStatus 状态:1已付款2已完成
	* @param crrrdRemarks 备注
	* @return
	*/
	@RequestMapping(value="/update/coreRideRecord", method=RequestMethod.POST)
	public void updateCoreRideRecord (String crrrdUuid, Integer crrrdStatus, String crrrdRemarks, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin updateCoreRideRecord");
		if (StringUtil.isEmpty(crrrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRideRecord coreRideRecord = new CoreRideRecord();
		coreRideRecord.setCrrrdUuid(crrrdUuid);
		coreRideRecord.setCrrrdUdate(new Date());
		coreRideRecord.setCrrrdStatus(crrrdStatus);
		coreRideRecord.setCrrrdRemarks(crrrdRemarks);

		coreRideRecordService.updateCoreRideRecord(coreRideRecord);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreRideRecordController]:end updateCoreRideRecord");

	}

	/**
	* 删除
	*
	* @param crrrdUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreRideRecord (String crrrdUuid, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin deleteCoreRideRecord");
		if (StringUtil.isEmpty(crrrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRideRecord coreRideRecord = new CoreRideRecord();
		coreRideRecord.setCrrrdUuid(crrrdUuid);

		coreRideRecordService.deleteCoreRideRecord(coreRideRecord);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreRideRecordController]:end deleteCoreRideRecord");

	}

	/**
	* 批量删除
	*
	* @param crrrdUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreRideRecord (String crrrdUuids, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin deleteBatchCoreRideRecord");
		if (StringUtil.isEmpty(crrrdUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crrrdUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreRideRecordService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreRideRecordController]:end deleteBatchCoreRideRecord");

	}

	/**
	* 获取单个
	*
	* @param crrrdUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreRideRecord (String crrrdUuid, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin viewsCoreRideRecord");
		if (StringUtil.isEmpty(crrrdUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRideRecord coreRideRecord = new CoreRideRecord();
		coreRideRecord.setCrrrdUuid(crrrdUuid);

		coreRideRecord = coreRideRecordService.getCoreRideRecord(coreRideRecord);

		CoreRideRecordVO coreRideRecordVO = new CoreRideRecordVO();
		coreRideRecordVO.convertPOToVO(coreRideRecord);
		
		//根据凑热闹UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideRecord.getCrrrdActive());
		coreRideRecordVO.setList(attachmentslist);
				writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreRideRecordVO), response);
		logger.info("[CoreRideRecordController]:end viewsCoreRideRecord");

	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCoreRideRecordList (HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin findCoreRideRecordList");
		List<CoreRideRecord> lists = coreRideRecordService.findCoreRideRecordList();
		List<CoreRideRecordVO> vos = new ArrayList<CoreRideRecordVO>();
		CoreRideRecordVO vo;
		for (CoreRideRecord coreRideRecord : lists) {
			vo = new CoreRideRecordVO();
			vo.setCrrrdUuid(coreRideRecord.getCrrrdUuid());
			vo.setCrrrdUser(coreRideRecord.getCrrrdUser());
			vo.setCrrrdActive(coreRideRecord.getCrrrdActive());
			vo.setCrrrdTitle(coreRideRecord.getCrrrdTitle());
			vo.setCrrrdCdate(DateUtil.formatTimesTampDate(coreRideRecord.getCrrrdCdate()));
			vo.setCrrrdUdate(DateUtil.formatTimesTampDate(coreRideRecord.getCrrrdUdate()));
			vo.setCrrrdStatus(coreRideRecord.getCrrrdStatus());
			vo.setCrrrdTotal(coreRideRecord.getCrrrdTotal());
			vo.setCrrrdRemarks(coreRideRecord.getCrrrdRemarks());
			//根据凑热闹UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideRecord.getCrrrdActive());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CoreRideRecordController]:end findCoreRideRecordList");

	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数

	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreRideRecordPage (Integer pageNum, Integer pageSize, String crrrdTitle, String crrrdUser, HttpServletResponse response) {
		logger.info("[CoreRideRecordController]:begin findCoreRideRecordPage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreRideRecord> page = coreRideRecordService.findCoreRideRecordPage(pageNum, pageSize, crrrdTitle, crrrdUser);
		Page<CoreRideRecordVO> pageVO = new Page<CoreRideRecordVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreRideRecordVO> vos = new ArrayList<CoreRideRecordVO>();
		CoreRideRecordVO vo;
		for (CoreRideRecord coreRideRecord : page.getResult()) {
			vo = new CoreRideRecordVO();
			vo.setCrrrdUuid(coreRideRecord.getCrrrdUuid());
			vo.setCrrrdUser(coreRideRecord.getCrrrdUser());
			vo.setCrrrdUserName(coreRideRecord.getCrrrdUserName());
			vo.setCrrrdActive(coreRideRecord.getCrrrdActive());
			vo.setCrrrdTitle(coreRideRecord.getCrrrdTitle());
			vo.setCrrrdCdate(coreRideRecord.getCrrrdCdate()!=null?DateUtil.formatTimesTampDate(coreRideRecord.getCrrrdCdate()):"");
			vo.setCrrrdUdate(coreRideRecord.getCrrrdUdate()!=null?DateUtil.formatTimesTampDate(coreRideRecord.getCrrrdUdate()):"");
			vo.setCrrrdStatus(coreRideRecord.getCrrrdStatus());
			vo.setCrrrdTotal(coreRideRecord.getCrrrdTotal());
			vo.setCrrrdRemarks(coreRideRecord.getCrrrdRemarks());

			//根据社区活动UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreRideRecord.getCrrrdActive());
			vo.setList(attachmentslist);
						vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreRideRecordController]:end findCoreRideRecordPage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}